<template>
  <base-breadcrumb class="" :title="titleName">
    <!-- 方案审核师管理 -->
    <a-spin :spinning="loading">
      <!-- 表格 -->
      <a-card :bordered="false">
          <title-name title="基本信息"></title-name>
          <custom-form
            ref="customForm"
            :form="form"
            :edit-model="'E'"
            :form-list="formList"
            :rules="rules"
            is-validate
            @onSelectChange="personChange"
            @selectChange="orgChange"
            @change="orgChange"
          />
        
          <!-- <a-form-model
            ref="ruleForm"
            :model="form"
            :rules="rules"
            :labelCol="{ span: 6 }"
            :wrapperCol="{ span: 18 }"
          >
            <a-row type="flex">
              <a-col :span="8">
                <a-form-model-item label="所属组织" prop="orgId">
                  <SelectSubCompany
                    v-model="form.orgId"
                    @selectChange="orgChange"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所属项目">
                  <SelectProject
                    v-model="form.projectId"
                    :disabled="!editable"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="姓名" prop="employeeId">
                  <SelectProject
                    v-model="form.employeeId"
                    :disabled="!editable"
                    :orgId="curOrg"
                    :init="true"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="性别">
                  <BaseInput
                    v-model="form.employeeId"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="电话号码">
                  <BaseInput
                    v-model="form.phone"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="职务">
                  <BaseInput
                    v-model="form.post"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="职称">
                  <BaseInput
                    v-model="form.jobtitle"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="第一学历">
                  <BaseInput
                    v-model="form.first"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="第二学历">
                  <BaseInput
                    v-model="form.second"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="最高学历专业">
                  <BaseInput
                    v-model="form.max"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="年龄">
                  <BaseInput
                    v-model="form.age"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="状态">
                  <BaseInput
                    v-model="form.status"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="聘用状态">
                  <BaseInput
                    v-model="form.hireStatus"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="备注">
                  <BaseTextarea
                    v-model="form.remark"
                    :maxLength="20"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model> -->
      </a-card>
    </a-spin>
    <!-- 页脚操作栏 -->
    <div class="btn_box page-btn-right-top">
      <a-button :disabled="loading" :loading="subloading" v-if="editable" type="primary" class="button_right" @click="handleSubmit(1)"
        >提交</a-button
      >
      <a-button @click="$close">关闭</a-button>
    </div>
    </base-breadcrumb>
</template>

<script>

import { addReviewer, updateReviewer, reviewerDetail } from '@/api/technical/review'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import SelectOrgPeople from '@/components/Select/SelectOrgPeople' // 人员
export default {
    name:'a'+Date.now(),
    components: {
      SelectSubCompany,
      SelectProject,
      SelectOrgPeople
    },
    data() {
        return {
            loading: false, // 加载控件
            defloading: false,
            subloading: false,
            form: {
              orgId: '',
              orgName: '',
              projectId: '',
              projectName: '',
              userId: undefined,
            }, // 表单
            rules: {
                // orgId: [{ required: true, message: '请选择所属组织', trigger: 'change' }],
                userId: [{ required: true, message: '请选择姓名', trigger: 'change' }],
            }, // 校验规则
            curOrg: '',
            // ownOrgList: []
        }
    },

    mounted() {
      if (this.type == 'add') {
        this.form.hireStatus = '0'
        this.form.orgId = this.ownOrgList.find((v) => {
          return v.code == this.curTagCode
        })?.id
      } else {
        this.getDetail()
      }
    },
    computed: {
      type() {
        return this.$route.query.type
      },
      editable() {
        return this.type == 'add' || this.type == 'edit'
      },
      titleName() {
        const nameMap = {
          'add': '添加方案审核师',
          'edit': '编辑方案审核师',
          'look': '方案审核师详情'
        }
        return nameMap[this.type] || '方案审核师详情'
      },
      curTagCode() {
        return this.$currentPost().tagCode
      },
      curEmployee() {
        return this.$userInfo().employee.levels
      },
      ownOrgList() {
        let levels = [...this.$userInfo().employee.levels]
        let ownOrg = levels.filter((v) => {
          return v.code === 'CO' || v.code === 'BC'
        })
        ownOrg.forEach((v) => {
          if (v.code == 'CO') v.name = '直属机关'
        })
        return ownOrg
      },
      formList() {
        return [
          {
            type: 'a-select',
            keyName: 'orgId',
            label: '所属机构',
            data: this.ownOrgList,
            replaceFields: {
              label: 'name',
              value: 'id'
            },
            change: (e) => {
            }
          },
          {
            type: SelectProject,
            keyName: 'projectId',
            viewName: 'projectName',
            label: '项目',
            disabled: (!this.editable),
            attrs: {
              allowClear: true,
              autoSelected: false, //this.editable,
              keyValue: 'orgId',
              // isEdit: true,
              form: this.form
            },
            selectChange: (e) => {
              console.log(e)
              // this.form.projectInfo = e
            }
          },
          {
            type: SelectOrgPeople,
            keyName: 'userId',
            // viewName: 'employeeName',
            disabled: (!this.editable),
            label: '姓名',
            isEdit: this.editable,
            attrs: {
              orgId: this.form.orgId,
              showSearch: true,
              // init: true
            },
            onSelectChange: (e) => {
              console.log(e)
            }
          },
          {
            keyName: 'sex',
            type: 'a-select',
            disabled: true,
            label: '性别',
            data: [
              {label: '男', value: '1'},
              {label: '女', value: '0'}
            ]
          },
          {
            keyName: 'phone',
            type: 'a-input',
            disabled: true,
            label: '电话号码',
          },
          {
            keyName: 'post',
            type: 'a-input',
            disabled: true,
            label: '职务',
          },
          {
            keyName: 'jobTitle',
            type: 'a-input',
            disabled: true,
            label: '职称',
          },
          {
            keyName: 'firstEducation',
            type: 'a-input',
            disabled: true,
            label: '第一学历',
          },
          {
            keyName: 'secondEducation',
            type: 'a-input',
            disabled: true,
            label: '第二学历',
          },
          {
            keyName: 'specialitieMax',
            type: 'a-input',
            disabled: true,
            label: '最高学历专业',
          },
          {
            keyName: 'age',
            type: 'a-input',
            disabled: true,
            label: '年龄',
          },
          {
            keyName: 'status',
            type: 'a-select',
            disabled: true,
            label: '状态',
            data: [
              {label: '在职', value: 1},
              {label: '离职', value: 0}
            ]
          },
          {
            keyName: 'hireStatus',
            type: 'a-select',
            disabled: true,
            label: '聘用状态',
            data: [
              {label: '聘用', value: '1'},
              {label: '非聘用', value: '0'}
            ]
          },
          {
            keyName: 'result',
            span: 24,
            type: 'BaseTextarea',
            disabled: (!this.editable),
            label: '备注',
            attrs: {
              maxLength: 20
            }
          },
        ]
      }
    },
    methods: {
      getDetail() {
        reviewerDetail(this.$route.query.id).then((res) => {
          if (res.code == 200) {
            const data = res.data
            data.hireStatus = res.data.hireStatus ? '1' : '0'
            this.form = data
          }
        })
      },
      personChange(e, v) {
        const data = { ...v }
        data.post = v?.postPNames?.join(',')
        data.employeeId = v.id
        data.employeeName = v.name
        this.setPersonInfo(data)
      },
      setPersonInfo(data) {
        this.form.employeeId = data.employeeId
        this.form.employeeName = data.employeeName
        this.form.sex = data.sex
        this.form.post = data.post
        this.form.phone = data.phone
        this.form.jobTitle = data.jobTitle
        this.form.firstEducation = data.firstEducation
        this.form.secondEducation = data.secondEducation
        this.form.specialitieMax = data.specialitieMax
        this.form.age = data.age
        this.form.status = data.status
      },
      // orgChange(n, v) {
      //   if (!v) return
      //   if (n == 'projectId') {
      //     this.form.projectName = v.name
      //   } else if(n == 'orgId') {
      //     this.form.orgName = this.ownOrgList.find((w) => w.id == v)?.name
      //   }
      //   // this.curOrg = e.orgId
      // },
      handleSubmit() {
        this.$refs.customForm.validate((valid) => {
          if (valid) {
            console.log(this.form)
            const param = {
              orgId: this.form.orgId,
              orgName: this.form.orgName,
              projectId: this.form.projectId,
              projectId: this.form.projectId,
              result: this.form.result,
              hireStatus: this.form.hireStatus == '1' ? true : false,
              userId: this.form.userId,
              employeeId: this.form.employeeId,
              employeeName: this.form.employeeName,
              level: this.ownOrgList.find((v) => v.id == this.form.orgId)?.code
            }
            console.log(param)
            this.subloading = true
            const api = this.type == 'add' ? addReviewer : updateReviewer
            const msg = this.type == 'add' ? '添加' : '编辑'
            api(param).then((res) => {
              this.subloading = false
              if (res.code == 200) {
                this.$message.success(msg+'成功')
                this.$close()
              }
            }).catch(() => {
              this.subloading = false
            })
          }
        })
      }
    },
}
</script>

<style lang="less" scoped>
</style>